﻿Imports sonicERPDAL
Imports sonicERPinfo

Partial Public Class FornecedorLista
    Inherits System.Web.UI.Page
    Dim dal As New fornecedorDAL
    Dim entidade As New fornercedorInfo
    Dim data As DateTime
    Dim util As New Util

#Region "Sub e Funçoes"
    Public Function FormataData(ByVal Data)
        If Data <> "" Then
            Dim dt As DateTime = Data
            Data = dt.ToString("dd/MM/yyyy")

        End If
        Return Data
    End Function

    Public Sub tipofornecedor()
        ddltipofornecedor.Items.Add(New ListItem("Selecione", 0))
        ddltipofornecedor.Items.Add(New ListItem("JURIDICA", 1))
        ddltipofornecedor.Items.Add(New ListItem("FISICA", 2))

    End Sub

    Public Sub getfornecedor()
        Try

        
            If String.IsNullOrEmpty(txtfornecedor.Text) Then
                entidade.nomefornecedor = Convert.ToString(DBNull.Value)
            Else
                entidade.nomefornecedor = txtfornecedor.Text
            End If

            If txtdataininico.Text = "" Then
                Dim data As DateTime = DateTime.Today.AddMonths(-1)
                txtdataininico.Text = Convert.ToString(data.ToShortDateString)
                entidade.dtinicio = Convert.ToDateTime(txtdataininico.Text)
                txtdataininico.Text = FormataData(txtdataininico.Text)
            Else

                Dim culture As New System.Globalization.CultureInfo("pt-BR")
                Dim dt As String = Convert.ToDateTime(txtdataininico.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
                entidade.dtinicio = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
            End If


            If txtdatafim.Text = "" Then
                Dim data As DateTime = DateTime.Today.AddMonths(1)
                txtdatafim.Text = Convert.ToString(data.ToShortDateString)
                entidade.dt_fim = Convert.ToDateTime(txtdatafim.Text)
                txtdatafim.Text = FormataData(txtdatafim.Text)
            Else
                Dim culture As New System.Globalization.CultureInfo("pt-BR")
                Dim dt As String = Convert.ToDateTime(txtdatafim.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
                entidade.dt_fim = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
            End If

            If ddltipofornecedor.SelectedItem.Value = 0 Then
                entidade.tipofornecedor = Convert.ToString(DBNull.Value)
            Else
                entidade.tipofornecedor = Convert.ToString(ddltipofornecedor.SelectedItem.Text)

            End If

            gvList.DataSource = dal.getfornecedor(entidade)
            gvList.DataBind()
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub
#End Region

#Region "Eventos do Form"


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        pnl1.Visible = False

        If Not IsPostBack Then
            Try
              
                Dim paginavisivel As Boolean
                paginavisivel = util.validarpagina(7)
                If Not paginavisivel Then
                    FormsAuthentication.SignOut()
                    Response.Redirect(ResolveUrl("~/Login.aspx"))
                End If
                tipofornecedor()
                getfornecedor()


            Catch ex As Exception
                pnl1.Visible = True
                lblerro.Text = ex.Message
            End Try
        End If
    End Sub


    Protected Sub btnok_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnok.Click
        Try
            pnl1.Visible = False

            If txtdataininico.Text = "" OrElse txtdatafim.Text = "" Then
                txtdataininico.BackColor = Drawing.Color.White
                txtdatafim.BackColor = Drawing.Color.White
                getfornecedor()
                lblerro.Text = ""
                Return
            Else
                Dim di As DateTime
                DateTime.TryParseExact(txtdataininico.Text, "dd/MM/yyyy", Nothing, Nothing, di)
                Dim df As DateTime
                DateTime.TryParseExact(txtdatafim.Text, "dd/MM/yyyy", Nothing, Nothing, df)


                If di = DateTime.MinValue Then
                    txtdataininico.Focus()
                    txtdataininico.BackColor = Drawing.Color.Aqua
                    txtdatafim.BackColor = Drawing.Color.White
                    Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                Else
                    txtdataininico.BackColor = Drawing.Color.White

                    If df = DateTime.MinValue Then
                        txtdatafim.Focus()
                        txtdatafim.BackColor = Drawing.Color.Aqua
                        txtdataininico.BackColor = Drawing.Color.White
                        Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                    Else
                        txtdatafim.BackColor = Drawing.Color.White
                    End If
                End If
            End If

            getfornecedor()
            lblerro.Text = ""
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    Private Sub gvList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvList.PageIndexChanging
        Try
            e.Cancel = True
            gvList.PageIndex = e.NewPageIndex
            getfornecedor()

        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    Protected Sub gvList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvList.RowCommand
        If e.CommandName = "Editar" Then
            Dim i As Integer = Convert.ToInt32(e.CommandArgument)
            Dim gv As GridViewRow = gvList.Rows(i)
            Dim url As String = "~/adm/CadastroFornecedor.aspx?id=" + Server.HtmlDecode(gv.Cells(1).Text)
            Response.Redirect(ResolveUrl(url))
        End If

    End Sub

  
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Response.Redirect(ResolveUrl("~/adm/CadastroFornecedor.aspx"))
    End Sub

    Protected Sub btnimprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnimprimir.Click
        Dim tipofornecedor As String = ""

        If ddltipofornecedor.SelectedValue <> "0" Then
            tipofornecedor = ddltipofornecedor.SelectedItem.Text

        End If
        Dim javascript As String = "<script language ='javascript'>"
        javascript += " window.open('Imprimir.aspx?idrelatorio=1&nome=" + Server.HtmlDecode(txtfornecedor.Text) + "&dtinicio=" + Server.HtmlDecode(txtdataininico.Text) + "" +
      "&dtfim=" + Server.HtmlDecode(txtdatafim.Text) + "&tipo=" + Server.HtmlDecode(tipofornecedor) + " ', '', 'letf=0,top=0,width=900px,height=900px,toolbar=0,scrollbars=1,status=0');"
        javascript += " </script>"
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "imprimir", javascript)
    End Sub






#End Region

    
   
   
  
End Class